<?php 
class AskController extends CommonController{
	/*
	 * 显示问题
	 **/
	 public function index(){
	 	//获得顶级分类**********
	 	$this->topCate();
		//获得问题的数据
		$asid = isset($_GET['asid']) ? $_GET['asid']:0;
		$askData=M()->query("SELECT u.uid,u.exp,a.solve,a.content,u.username,a.time FROM hd_ask AS a JOIN hd_user AS u ON a.uid=u.uid WHERE asid={$asid}");
		$askAccept=M()->query("SELECT * FROM hd_answer  WHERE asid={$asid} and accept=1");
		$this->assign('askAccept',$askAccept);
		$askData[0]['lv']=$this->expToLv($askData[0]['exp']);
		$total=M()->query("SELECT count(*) FROM hd_answer WHERE asid={$asid}");
		$askData[0]['total']=$total[0]['count(*)'];
		
		$this->assign('askData',$askData[0]);
		//获得当前问题的答案******************
		$answerData=M()->query("SELECT * FROM hd_answer WHERE asid={$asid}");
//		p($answerData);
		$this->assign('answerData',$answerData);
		//右侧数据处理*********************
		//登陆以后用户的数据处理
		//$userInfo = M()->query("SELECT * FROM hd_user WHERE uid='{$_SESSION['uid']}'");
//		判断SESSION是否存在执行下面的内容
		if(isset($_SESSION['uid']) && isset($_SESSION['uname'])){
			//查询所有的用户
			$sql="SELECT * FROM hd_user WHERE uid='{$_SESSION['uid']}'";
			
			$userData=M()->query($sql);
			
			if($userData[0]['answer']!=0){
				$userData[0]['cnl']=$userData[0]['accept']/$userData[0]['answer'] *100 . "%";
				$userData[0]['cnl']=round($userData[0]['cnl'],2) . "%";
			}else{
				$userData[0]['cnl']=0;
			}
			$userData[0]['lv']=$this->expToLv($userData[0]['exp']);
//			p($userData);
			$this->assign('userData',$userData[0]);
			
		}
		
		// 每日回答问题最多的人*********************
		$todayUid = $this->dayStar();
		$uid = $todayUid[0]['uid'];
		$TodayAns = M()->query("SELECT * FROM hd_user WHERE uid={$uid}");
		$TodayAns[0]['lv'] = $this->expToLv($TodayAns[0]['exp']);
		$this->assign('TodayAns',$TodayAns);
		//p($TodayAns);
		
		//（历史记录）***********
		//接收这个值
		$historyArr=$this->historyStar();
		$historyArr = $historyArr[0];
		//它的经验对应的是经验值
		$historyArr['lv']=$this->expToLv($historyArr['exp']);
		
		$historyArr['accept_l']=$historyArr['accept']/$historyArr['answer']*100 .'%';
		$historyArr['accept_l']=round($historyArr['accept_l'],2) .'%';
		//p($historyArr);
		//分配出去
		$this->assign('historyArr',$historyArr);
		
			
		//(后盾问答助人光荣榜)*************
		$helpArr = $this->helpPeople();
		$this->assign('helpArr',$helpArr);
		
		
		//获得相关问题********
		$questionData=M()->query("SELECT * FROM hd_ask WHERE cid=(SELECT cid FROM hd_ask WHERE asid={$asid})");
		$this->assign('questionData',$questionData);
		//通过知道问题（$askData）的cid，找到cid下面的一些问题
		$this->display();
		
	 }

	  
	  /*
	   * 回答
	   * */
	   public function answer(){

//		
		if(IS_POST){
	   		$content = $_POST['content'];
		    $time = time();
			$uid = $_SESSION['uid'];	
			$asid = $_GET['asid'];
			//插入回答表
	   		M()->exec("insert into hd_answer (content,time,uid,asid) values('{$content}',{$time},{$uid},{$asid})");
			//修改用户名一样
		    M()->exec("update hd_user set answer=answer+1 where uid={$uid}");
			//修改问题表，必须所属问题一样
			M()->exec("update hd_ask set answer=answer+1 where asid={$asid}");
			$this->success('回答成功！！',"index.php?c=Ask&asid={$asid}");
	   }
		
		
		
	   }
	 
	 	 /*
	  * 采纳
	  * */
	  public function accept(){
	  	
		//获取uid
		$uid = $_GET['uid'];
		//获取asid
		$asid = $_GET['asid'];
		//获取anid
		$anid = $_GET['anid'];
		//获取悬赏值
		$point = M()->query("SELECT reward FROM hd_ask WHERE asid={$asid}");
		//p($point);die;
		//1.把答案表的accept字段改为1就是，采纳了
   		M()->exec("update hd_answer set accept=1 where anid={$anid}");
		//3.给被采纳用户加采纳数，金币，经验
	    M()->exec("update hd_user set accept=accept+1,point=point+{$point[0]['reward']} where uid={$uid}");
	    //2.把问题表标记为已解决
		M()->exec("update hd_ask set solve=1 where asid={$asid}");
		$this->success('正确答案！！',"index.php?c=Ask&asid={$asid}");
	
		
	  }
	  
	  /*
	   * 提问操作
	   * */
	 public function ask(){
	 	$this->topCate();
		
		if(IS_POST){
			//如果没有登陆
			if(!isset($_SESSION['uid']) || !isset($_SESSION['uname'])){
				$this->error('请登录','index.php');
			}
			
			//提问的sql插入hd_ask表
			//1内容
			$content=$_POST['content'];
			//2时间
			$time=time();
			//3悬赏金币
			$reward=$_POST['reward'];
			//4分类的cid
			$cid=(int)$_POST['cid'];
			//5用户的uid
			$uid=$_SESSION['uid'];
			//给hd_ask表添加数据
			$asid=M()->exec("INSERT INTO hd_ask SET content='{$content}',time={$time},reward={$reward},cid={$cid},uid={$uid}");
			//给user表插入数据
			$sql=M()->exec("UPDATE hd_user SET ask=ask+1,exp=exp+5,point=point-{$reward} WHERE uid={$uid}");
//			p(123);die;
			//提问成功
			$this->success("提交问题成功",U('Ask/index')."&asid=".$asid);
			
		}
		$this->display();
	 }
	 /*
	  * 获得子集分类
	  * */
	 public function getSon(){
	 	//此id是ask.html异步的cid的下标
	 	$cid = (int)$_POST['id'];
		$sql="SELECT * FROM hd_category WHERE pid={$cid}";
		$data = M()->query($sql);
		//把数组按照json返回
		echo json_encode($data);
	 }
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
}



































 ?>